﻿CREATE TRIGGER [dbo].[tr_Borrower_SSN]
ON [dbo].[t_Borrower] 
FOR INSERT, UPDATE /*TODO: Handle deleting Borrower that belongs to multiple Loans*/
AS
RETURN
DECLARE @RC int SET @RC = @@ROWCOUNT
IF @RC = 0 RETURN
IF TRIGGER_NESTLEVEL(@@PROCID) > 1 RETURN
DECLARE @BorrowerID int SELECT @BorrowerID = ID FROM inserted
DECLARE @IsEdit bit,@SSN_Old sysname SELECT @IsEdit = 0
IF UPDATE (SSN) BEGIN
	SELECT @SSN_Old = SSN FROM deleted
	EXEC s_Borrower_Process_SSN @BorrowerID,@SSN_Old,@IsEdit
	IF @@ERROR <> 0 RETURN

	INSERT INTO v_Borrower_CheckVIN	SELECT ID,0 FROM inserted
	IF @@ERROR <> 0 RETURN

	UPDATE CI SET Pulled = 0
	FROM t_CreditInfo CI INNER JOIN inserted i ON i.ID = CI.BorrowerID
	WHERE CI.Pulled <> 0
END


